# --------------------------------------------------------------------------------
#  NoiseTube Web application
#  
#  Copyright (C) 2008-2010 SONY Computer Science Laboratory Paris
#  Portions contributed by Vrije Universiteit Brussel (BrusSense team), 2008-2011
# --------------------------------------------------------------------------------
#  This library is free software; you can redistribute it and/or modify it under
#  the terms of the GNU Lesser General Public License, version 2.1, as published
#  by the Free Software Foundation.
#  
#  This library is distributed in the hope that it will be useful, but WITHOUT
#  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
#  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
#  details.
#  
#  You should have received a copy of the GNU Lesser General Public License along
#  with this library; if not, write to:
#    Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor,
#    Boston, MA  02110-1301, USA.
#  
#  Full GNU LGPL v2.1 text: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
#  NoiseTube project source code repository: http://code.google.com/p/noisetube
# --------------------------------------------------------------------------------
#  More information:
#   - NoiseTube project website: http://www.noisetube.net
#   - Sony Computer Science Laboratory Paris: http://csl.sony.fr
#   - VUB BrusSense team: http://www.brussense.be
# --------------------------------------------------------------------------------
class Cartasur::VisitedsController < Cartasur::ApplicationController
  include Geokit::Geocoders

  def index
      @cusers = Cartasur::User.all
    @visiteds = Cartasur::Visited.all
  end

  def new
   @visited = Cartasur::Visited.new
  end


  def create
    visited_hash = params[:cartasur_visited]
    unless visited_hash.nil?
        #set protocol id
        protocol = Cartasur::Protocol.where("users_id = #{visited_hash[:user_id]} and objective_id = #{visited_hash[:objective_id]}").try(:first)
        visited_hash[:protocol_id] =  protocol.try(:id)
         @visited = Cartasur::Visited.new(visited_hash)
         if @visited.save
           flash[:notice] = "Successfully saved!"
           redirect_to @visited
         else
           flash[:alert] = "There was a problem when saving"
           render :action => 'new'
         end
    end

  end

  def edit
    @visited  = Cartasur::Visited.find(params[:id])

  end
  def update
    @visited = Cartasur::Visited.find(params[:id])

    if @visited.update_attributes(params[:cartasur_visited])
      flash[:notice] = "The item was successfully updated!"
      redirect_to @visited
    else
      flash[:alert] = "There was a problem when updating"
    end
  end

  def show
    @visited  = Cartasur::Visited.find(params[:id])
  end

  def summary
    if params[:id] == "0"
      flash[:notice] = "The record was not found"
      redirect_to cartasur_user_path(current_user)
    else
      @visited = Cartasur::Visited.find(params[:id])

      @vimages = Cartasur::VisitedsImage.where(:visited_id => @visited).all

      @user = @visited.protocol.user

      @answers = @visited.protocol.answers
      @questions = Cartasur::Question.get_questions

    end
  end
end
